package 二叉树相关问题.中度题;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class 路径总和2 {
    List<List<Integer>> lists = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if (root==null)
            return lists;
        List<Integer> list = new ArrayList<>();
        bfs(root,targetSum,list);
        return lists;
    }

    private void bfs(TreeNode root, int targetSum, List<Integer> list) {
        if (root==null)
            return;
        list.add(root.val);
        if (root.val==targetSum && root.left==null && root.right==null){
            lists.add(new ArrayList<>(list));
        }
        bfs(root.left,targetSum-root.val, list);
        bfs(root.right,targetSum-root.val, list);
        list.remove(list.size()-1);
    }
}
